/**
 * Copyright phonemap.com
 *
 * Licensed under the phonemap.com License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.phonemap.com/licenses/LICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonemap.common;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.GeoPt;
import com.phonemap.models.User;

/**
 * The Class InitEntity.
 */
public final class InitEntity {

	/** The Constant MAX_INPUT. */
	private static final int MAX_INPUT = 5;

	/**
	 * Instantiates a new inits the entity.
	 */
	private InitEntity() {

	}

	/**
	 * Insert users.
	 */
	public static void insertUsers() {
		Iterable<Entity> oldUsers = User.getUsersWithoutPosition();
		List<String> positions = getPositions("C:\\workspace\\new\\phonemap\\war\\position.txt");
		int start = 0;
		for (Entity user : oldUsers) {

			if ((start + MAX_INPUT) < positions.size()) {

				// Gets 5 positions to update into User Entity
				Calendar cal = Calendar.getInstance();
				int days = MAX_INPUT;
				for (int i = start; i < (start + MAX_INPUT); i++) {
					String pos = positions.get(i);
					String[] point = pos.split(",");
					cal = Calendar.getInstance();
					cal.setTimeZone(TimeZone.getTimeZone("GMT-7"));
					days--;
					cal.add(Calendar.DAY_OF_MONTH, -days);
					 User.updatePosition(
					 user.getKey().getId(),
					 new GeoPt(Float.parseFloat(point[0]), Float
					 .parseFloat(point[1])), cal.getTime());
//					Position.insertPosition(
//							user.getKey().getId(),
//							new GeoPt(Float.parseFloat(point[0]), Float
//									.parseFloat(point[1])), cal.getTime());
				}

				// Inserts current day.
//				String pos = positions.get(start + MAX_INPUT);
//				String[] point = pos.split(",");
//				cal = Calendar.getInstance();
//				cal.setTimeZone(TimeZone.getTimeZone("GMT-7"));
//
//				User.updatePosition(
//						user.getKey().getId(),
//						new GeoPt(Float.parseFloat(point[0]), Float
//								.parseFloat(point[1])), cal.getTime());

				start = start + MAX_INPUT;
			}

		}

	}

	/**
	 * Gets the positions.
	 * 
	 * @param filename
	 *            the filename
	 * @return the positions
	 */
	private static List<String> getPositions(final String filename) {
		List<String> positions = new ArrayList<String>();

		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(filename));
			String line = br.readLine();

			while (line != null) {
				positions.add(line);
				line = br.readLine();
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return positions;
	}

}
